/*
 * Created by JFormDesigner on Tue Dec 17 00:17:29 CST 2024
 */

package findbook;

import java.awt.event.*;
import javax.swing.*;
import net.miginfocom.swing.*;

import java.awt.event.MouseEvent;
import java.sql.*;

/**
 * @author jjchen
 */
public class findbook_authorandbookname extends JFrame {

    public findbook_authorandbookname() {
        initComponents();
    }

    private JTextArea resultArea;
    private void buttonMouseClicked(MouseEvent e) {
        this.resultArea = new JTextArea();
        this.add(this.resultArea);

        String bookName = textField2.getText();
        String writerName = textField1.getText();

        String url = "jdbc:mysql://localhost:3306/javaproject";
        String username = "root";
        String password = "root";
        String sql = "SELECT * FROM book_information WHERE book_title=? and writer=?";


        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;

        try {
            // 加载MySQL JDBC驱动（确保已添加MySQL JDBC驱动到项目中）
            Class.forName("com.mysql.cj.jdbc.Driver");
            // 建立数据库连接
            conn = DriverManager.getConnection(url, username, password);
            // 创建PreparedStatement对象并执行查询
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, bookName);
            pstmt.setString(2, writerName);
            rs = pstmt.executeQuery();

            // 如果查询到结果，则显示结果；否则显示未找到

            if (rs.next()) {
                StringBuilder sb = new StringBuilder();
                // 假设您想显示书名和作者，您可以根据需要添加更多字段
                sb.append("id: ").append(rs.getString("book_id")).append("\n");
                sb.append("书名: ").append(rs.getString("book_title")).append("\n");
                sb.append("作者: ").append(rs.getString("writer")).append("\n");
                sb.append("入库时间: ").append(rs.getString("storage_time")).append("\n");

                // ... 其他字段的显示
                resultArea.setText(sb.toString()); // 在文本区域显示查询结果
            } else {
                resultArea.setText("未找到该图书的信息。");
            }
        } catch (ClassNotFoundException | SQLException ex) {
            // 处理JDBC相关的异常
            JOptionPane.showMessageDialog(this, "数据库连接或查询出错！\n" + ex.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
        } finally {
            // 关闭资源
            try {
                if (rs != null) rs.close();
                if (pstmt != null) pstmt.close();
                if (conn != null) conn.close();
            } catch (SQLException ex) {
                // 处理关闭资源时可能抛出的异常
                JOptionPane.showMessageDialog(this, "关闭资源时出错！\n" + ex.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
            }
        }

    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        label2 = new JLabel();
        textField2 = new JTextField();
        label1 = new JLabel();
        textField1 = new JTextField();
        button1 = new JButton();

        //======== this ========
        setAutoRequestFocus(false);
        var contentPane = getContentPane();
        contentPane.setLayout(new MigLayout(
            "hidemode 3",
            // columns
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]",
            // rows
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]"));

        //---- label2 ----
        label2.setText("\u4f5c\u8005\u540d\u79f0");
        contentPane.add(label2, "cell 0 2");
        contentPane.add(textField2, "cell 4 2 9 1");

        //---- label1 ----
        label1.setText("\u4e66\u7c4d\u540d\u79f0");
        contentPane.add(label1, "cell 0 3");
        contentPane.add(textField1, "cell 4 3 9 1");

        //---- button1 ----
        button1.setText("\u641c\u7d22");
        button1.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                buttonMouseClicked(e);
            }
        });
        contentPane.add(button1, "cell 4 6");
        setSize(650, 390);
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JLabel label2;
    private JTextField textField2;
    private JLabel label1;
    private JTextField textField1;
    private JButton button1;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
